import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'base_refresher_controller.dart';
import 'smart_refresher_by_flow.dart';
import 'smart_refresher_by_grid.dart';
import 'smart_refresher_by_list.dart';

enum ListType { list, flow, grid }

class BaseRefresherView extends StatefulWidget {
  const BaseRefresherView({
    this.header,
    required this.item,
    required this.apiFn,
    this.needAnimation = true,
    this.refreshFooterColor,
    this.padding,
    this.crossAxisSpacing = 0,
    this.mainAxisSpacing = 0,
    this.useFraming = true,
    this.isFlex = false,
    this.listType = ListType.list,
    this.crossAxisCount = 1,
  });

  final Widget Function(dynamic data) item;
  final Function(int pageIndex) apiFn;
  final ListType? listType;
  final int crossAxisCount;

  final Widget? header;

  //是否需要下拉动画
  final bool needAnimation;
  final Color? refreshFooterColor;
  //间距的问题
  final EdgeInsetsGeometry? padding;

  //流式布局Grid布局 需要的 顶部广告是否 全部占用 默认全部占用
  final bool isFlex;

  //纵向间距
  final double crossAxisSpacing;

  //横向间距
  final double mainAxisSpacing;

  //是否需要 分帧技术
  final bool useFraming;

  @override
  State<BaseRefresherView> createState() => _BaseRefresherViewState();
}

class _BaseRefresherViewState extends State<BaseRefresherView> {
  late BaseRefresherController ctrl = BaseRefresherController(
    apiFn: widget.apiFn,
  );
  final String tag = DateTime.now().microsecondsSinceEpoch.toString();
  late RefreshController refreshCtrl =
      RefreshController(initialRefresh: widget.needAnimation);
  @override
  void initState() {
    super.initState();

    //如果不需要动画 则主动加载
    if (!widget.needAnimation) {
      ctrl.onRefresh(refreshCtrl: refreshCtrl);
    }
  }

  @override
  Widget build(BuildContext context) {
    Widget smartRefresher = Container();

    switch (widget.listType) {
      case ListType.list:
        smartRefresher = SmartRefresherByList(
          padding: widget.padding ?? const EdgeInsets.all(10),
          useFraming: widget.useFraming,
          itmList: widget.header != null
              ? ctrl.dataList.length + 1
              : ctrl.dataList.length,
          controller: refreshCtrl,
          onRefresh: () => ctrl.onRefresh(refreshCtrl: refreshCtrl),
          onLoading: () => ctrl.onLoading(refreshCtrl: refreshCtrl),
          child: (context, index) {
            var i = index;
            if (widget.header != null) {
              i--;
            }
            if (widget.header != null && index == 0) {
              return widget.header;
            }

            final dynamic data = ctrl.dataList[i];
            return widget.item(data);
          },
        );
        break;
      case ListType.grid:
        smartRefresher = SmartRefresherByGrid(
          useFraming: widget.useFraming,
          header: widget.header,
          refreshFooterColor: widget.refreshFooterColor,
          crossAxisCount: widget.crossAxisCount,
          padding: widget.padding ?? const EdgeInsets.all(10),
          itmList: ctrl.dataList.isEmpty
              ? 0
              : ctrl.dataList.length + widget.crossAxisCount,
          controller: refreshCtrl,
          crossAxisSpacing: widget.crossAxisSpacing,
          mainAxisSpacing: widget.mainAxisSpacing,
          onRefresh: () => ctrl.onRefresh(refreshCtrl: refreshCtrl),
          onLoading: () => ctrl.onLoading(refreshCtrl: refreshCtrl),
          child: (context, index) {
            if (index < widget.crossAxisCount) {
              return Container();
            }
            final dynamic data = ctrl.dataList[index - widget.crossAxisCount];
            return widget.item(data);
          },
        );
        break;
      case ListType.flow:
        smartRefresher = SmartRefresherByFlow(
          useFraming: widget.useFraming,
          header: widget.header,
          crossAxisCount: widget.crossAxisCount,
          padding: widget.padding ?? const EdgeInsets.all(10),
          itmList: ctrl.dataList.length,
          controller: refreshCtrl,
          onRefresh: () => ctrl.onRefresh(refreshCtrl: refreshCtrl),
          onLoading: () => ctrl.onLoading(refreshCtrl: refreshCtrl),
          child: (context, index) {
            final dynamic data = ctrl.dataList[index];
            return widget.item(data);
          },
        );
        break;
      case null:
        break;
    }

    return SizedBox(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: smartRefresher,
    );
  }
}
